package com.njlime.member.repository.app;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.utils.StringUtil;
import com.njlime.member.entiies.TMember;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class AppMemberDaoImpl extends BaseDaoImpl<TMember> {

    /**
     * id 修改member
     * @param member
     * @return
     */
    public int updateById(TMember member){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member set update_time = now()");
        if(!StringUtil.isEmpty(member.getMobile())){
            sb.append(",mobile=   ?");
            arrList.add(member.getMobile());
        }
        if(member.getSex() != null){
            sb.append(",sex=   ? ");
            arrList.add(member.getSex());
        }
        if(!StringUtil.isEmpty(member.getBirthday())){
            sb.append(",birthday=  ? ");
            arrList.add(member.getBirthday());
        }
        if(member.getProvince_id() != null ){
            sb.append(",province_id=  ? ");
            arrList.add(member.getProvince_id());
        }
        if(!StringUtil.isEmpty(member.getProvince_name())){
            sb.append(",province_name=  ? ");
            arrList.add(member.getProvince_name());
        }
        if(member.getCity_id() != null){
            sb.append(",city_id=  ? ");
            arrList.add(member.getCity_id());
        }
        if(!StringUtil.isEmpty(member.getCity_name())){
            sb.append(",city_name=  ? ");
            arrList.add(member.getCity_name());
        }
        if(member.getArea_id() != null ){
            sb.append(",area_id=  ? ");
            arrList.add(member.getArea_id());
        }
        if(!StringUtil.isEmpty(member.getArea_name())){
            sb.append(",area_name=  ? ");
            arrList.add(member.getArea_name());
        }
        if(!StringUtil.isEmpty(member.getAddress())){
            sb.append(",address=  ? ");
            arrList.add(member.getAddress());
        }
        if(!StringUtil.isEmpty(member.getWx_email())){
            sb.append(",wx_email=  ? ");
            arrList.add(member.getWx_email());
        }
        if(!StringUtil.isEmpty(member.getName())){
            sb.append(",name=  ? ");
            arrList.add(member.getName());
        }
        if(!StringUtil.isEmpty(member.getNick_name())){
            sb.append(",nick_name=  ? ");
            arrList.add(member.getNick_name());
        }
        if(!StringUtil.isEmpty(member.getWx())){
            sb.append(",wx=  ? ");
            arrList.add(member.getWx());
        }
        if(!StringUtil.isEmpty(member.getOpenid())){
            sb.append(",openid=  ? ");
            arrList.add(member.getOpenid());
        }
        if(!StringUtil.isEmpty(member.getHead_url())){
            sb.append(",head_url=  ? ");
            arrList.add(member.getHead_url());
        }
        sb.append(" WHERE id=? ");
        arrList.add(member.getId());
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 修改openid
     * @param id
     * @param openid
     * @return
     */
    public int updateOpenid(Integer id, String openid){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member set openid = ? where id = ?");
        arrList.add(openid);
        arrList.add(id);
        return updateJdbc(sb.toString(),arrList);
    }

    /**
     * 修改联系方式
     * @param id
     * @param mobile
     * @return
     */
    public int updateMobile(Integer id, String mobile){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member set mobile = ? where id = ?");
        arrList.add(mobile);
        arrList.add(id);
        return updateJdbc(sb.toString(),arrList);
    }

    /**
     * 修改兴趣标签
     * @param id
     * @param tags
     * @return
     */
    public int updateTags(Integer id, String tags){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member set tags = ? where id = ?");
        arrList.add(tags);
        arrList.add(id);
        return updateJdbc(sb.toString(),arrList);
    }

    /**
     * 根据手机获取粉丝
     * @param mobile
     * @param unit_id
     * @return
     */
    public  Map<String,Object> getUserByMobile(String mobile,Integer unit_id) {
        Map<String,Object> map = null;
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("SELECT * from t_member a WHERE  a.mobile = ? and unit_id =  ? ");
        arrList.add(mobile);
        arrList.add(unit_id);
        List<Map<String,Object>> list = getListJdbc(sb.toString(), arrList);
        if(list != null && list.size()>0){
            map = list.get(0);
        }
        return map;
    }
   public TMember getUserByOpenId(String openid) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("SELECT * from t_member a WHERE  a.openid = ?  ");
        arrList.add(openid);
        List<TMember> list = findList(sb.toString(), arrList);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    /**
     * 修改密码
     * @param mobile
     * @param password
     */
    public void updatePsw(String mobile, Integer unit_id, String password) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member set  password =? WHERE mobile=? and unit_id = ? ");
        arrList.add(password);
        arrList.add(mobile);
        arrList.add(unit_id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 修改头像
     * @param sysId
     * @param pic
     */
    public void updateUserPic(Integer sysId, String pic) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member set head_url = ? WHERE id=? ");
        arrList.add(pic);
        arrList.add(sysId);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * id 查找member
     * @param id
     * @return
     */
    public Map<String,Object> getMemberById(Integer id){
        Map<String,Object> map = null;
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("SELECT a.* from t_member a WHERE  a.id = ? ");
        arrList.add(id);
        List<Map<String,Object>> list = getListJdbc(sb.toString(), arrList);
        if(list != null && list.size()>0){
            map = list.get(0);
        }
        return map;
    }

    /**
     * openid 查找member
     * @param openid
     * @return
     */
    public Map<String,Object> getWithOpenId(String openid, Integer unit_id){
        Map<String,Object> map = null;
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("SELECT a.* from t_member a WHERE a.openid = ? and unit_id = ? ");
        arrList.add(openid);
        arrList.add(unit_id);
        List<Map<String,Object>> list = getListJdbc(sb.toString(), arrList);
        if(list != null && list.size()>0){
            map = list.get(0);
        }
        return map;
    }

    /**
     * 更新时间
     * @param id
     */
    public void updateMemberUpdateTime(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member set update_time= now() WHERE id=? ");
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 更新粉丝阅读数
     * @param member_id
     */
    public void updateReadCount(Integer member_id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member ");
        sb.append("set read_count = IFNULL(read_count,0) + 1 ");
        sb.append("where ");
        sb.append("id = ? ");
        arrList.add(member_id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 更新粉丝订单数
     * @param member_id
     */
    public void updateOrderCount(Integer member_id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member ");
        sb.append("set order_count = IFNULL(order_count,0) + 1 ");
        sb.append("where ");
        sb.append("id = ? ");
        arrList.add(member_id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 粉丝升级
     * @param member_id
     * @param ori_level
     * @param new_level
     */
    public void upgradeVip(Integer member_id,Integer ori_level,Integer new_level){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_member set level_before = ?,level_now = ? where id = ? ");
        arrList.add(ori_level);
        arrList.add(new_level);
        arrList.add(member_id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 获取单位所有标签
     * @return
     */
    public List<Map<String, Object>> labelGet(Integer unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_label where unit_id = ? and is_del = 0 ");
        arrList.add(unit_id);
        return getListJdbc(sb.toString(), arrList);
    }

}
